package com.bocai.dahuatech.base;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.bocai.dahuatech.R;
import com.bocai.dahuatech.event.AsyncEvent;
import com.bocai.dahuatech.event.BackgroundEvent;
import com.bocai.dahuatech.event.MainEvent;
import com.bocai.dahuatech.event.PostEvent;
import com.bocai.dahuatech.netutil.MyRetrofit;
import com.bocai.dahuatech.netutil.TestApi;
import com.bocai.dahuatech.utils.MyProgressUtil;
import com.trello.rxlifecycle.components.support.RxFragment;

import de.greenrobot.event.EventBus;


/**
 * Created by Kai on 2015/7/20.
 */
public class BaseFragment extends RxFragment implements BaseView {
    private Dialog mDialog;
    public String start="{\"content\":";
    public String end="}";
    Toast mToast;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
//        getActivity().overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
    }
    @Override
    public void showLoading() {
        mDialog = MyProgressUtil.createLoadingDialog(getActivity(), R.string.loading);
        mDialog.setCanceledOnTouchOutside(false);
        mDialog.show();
    }
    public TestApi getTestApi(){
        return new MyRetrofit().getGirlApi();
    }
    @Override
    public void hideLoading() {
        if(mDialog!=null){
            mDialog.dismiss();
        }
    }

    @Override
    public void showError(String msg) {
        Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showToast(String msg) {
        if(mToast==null)
        {

            mToast= Toast.makeText(getActivity(), msg, 5000);

        }else{
            mToast.setText(msg);
            mToast.setDuration(5000);
        }
        mToast.show();
    }
    @Override
    public void showError(int msg) {
        Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showToast(int msg) {
        if(mToast==null)
        {

            mToast= Toast.makeText(getActivity(), msg, 5000);

        }else{
            mToast.setText(msg);
            mToast.setDuration(5000);
        }
        mToast.show();
    }



    @Override
    public void setData(String json) {

    }
    /**
     * 在发布线程运行
     * @param event
     */
    public void onEvent(PostEvent event) {
    }

    /**
     * 在主线程运行
     * @param event
     */
    public void onEventMainThread(MainEvent event) {
    }

    /**
     * 如果是主线程发布，则在子线程中运行，否则在发布线程运行
     * @param event
     */
    public void onEventBackgroundThread(BackgroundEvent event) {
    }

    /**
     * 总是新建子线程
     * @param event
     */
    public void onEventAsync(AsyncEvent event) {
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
